// @Software: serve
// @File : sys_casbin
// @Author : vince
// @Time : 2021/9/19 12:31
// @Description  请填写文件描述
package fake

import (
	"fmt"
	"gintify/core"
	"gintify/model"
	"gorm.io/gorm"
)

func FakeCasbin() {
	var rules = [][]string{
		{"Administrator", "/", "GET"},
		{"Administrator", "/api/v1/register", "POST"},
		{"Administrator", "/api/v1/login", "POST"},
		{"Administrator", "/api/v1/logout", "POST"},

		{"Administrator", fmt.Sprintf("/api/v1/%s", "roles"), "POST"},
		{"Administrator", fmt.Sprintf("/api/v1/%s", "roles"), "GET"},
		{"Administrator", fmt.Sprintf("/api/v1/%s/:id", "roles"), "GET"},
		{"Administrator", fmt.Sprintf("/api/v1/%s/:id", "roles"), "PUT"},
		{"Administrator", fmt.Sprintf("/api/v1/%s/:id", "roles"), "PATCH"},
		{"Administrator", fmt.Sprintf("/api/v1/%s/:id", "roles"), "DELETE"},

		{"Administrator", fmt.Sprintf("/api/v1/%s", "users"), "POST"},
		{"Administrator", fmt.Sprintf("/api/v1/%s", "users"), "GET"},
		{"Administrator", fmt.Sprintf("/api/v1/%s/:id", "users"), "GET"},
		{"Administrator", fmt.Sprintf("/api/v1/%s/:id", "users"), "PUT"},
		{"Administrator", fmt.Sprintf("/api/v1/%s/:id", "users"), "PATCH"},
		{"Administrator", fmt.Sprintf("/api/v1/%s/:id", "users"), "DELETE"},

		{"Administrator", fmt.Sprintf("/api/v1/%s", "apis"), "POST"},
		{"Administrator", fmt.Sprintf("/api/v1/%s", "apis"), "GET"},
		{"Administrator", fmt.Sprintf("/api/v1/%s/:id", "apis"), "GET"},
		{"Administrator", fmt.Sprintf("/api/v1/%s/:id", "apis"), "PUT"},
		{"Administrator", fmt.Sprintf("/api/v1/%s/:id", "apis"), "PATCH"},
		{"Administrator", fmt.Sprintf("/api/v1/%s/:id", "apis"), "DELETE"},

		{"Administrator", fmt.Sprintf("/api/v1/%s", "menus"), "POST"},
		{"Administrator", fmt.Sprintf("/api/v1/%s", "menus"), "GET"},
		{"Administrator", fmt.Sprintf("/api/v1/%s/:id", "menus"), "GET"},
		{"Administrator", fmt.Sprintf("/api/v1/%s/:id", "menus"), "PUT"},
		{"Administrator", fmt.Sprintf("/api/v1/%s/:id", "menus"), "PATCH"},
		{"Administrator", fmt.Sprintf("/api/v1/%s/:id", "menus"), "DELETE"},

		//{"Administrator", fmt.Sprintf("/api/v1/%s", "casbin"), "POST"},
		//{"Administrator", fmt.Sprintf("/api/v1/%s", "casbin"), "GET"},
		//{"Administrator", fmt.Sprintf("/api/v1/%s/:id", "casbin"), "GET"},
		//{"Administrator", fmt.Sprintf("/api/v1/%s/:id", "casbin"), "PUT"},
		//{"Administrator", fmt.Sprintf("/api/v1/%s/:id", "casbin"), "PATCH"},
		//{"Administrator", fmt.Sprintf("/api/v1/%s/:id", "casbin"), "DELETE"},
	}

	db := core.DB.Model(&model.SysCasbin{})
	_ = db.Transaction(func(tx *gorm.DB) error {
		if tx.First(&model.SysCasbin{}).RowsAffected > 0 {
			core.LG.Errorf("SysCasbin表不为空")
		}
		return nil
	})
	ok, err:=core.CE.AddPolicies(rules)
	if !ok || err != nil {
		core.LG.Errorf("Casbin策略添加失败, ok: %v err: %v\n", ok, err)
		return
	}
	//for _, rule := range rules {
	//	ok, err := core.CE.AddPolicy(rule)
	//	if !ok || err != nil {
	//		core.LG.Errorf("Casbin策略添加失败, ok: %v err: %v\n", ok, err)
	//		return
	//	}
	//}
}
